package edu.umbc.bluebarracudas;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.*;
import java.awt.datatransfer.*;

import javax.media.jai.*;
import javax.imageio.*;
import java.lang.Throwable;



/* @author Stephen Sullivan
 * @email step5@umbc.edu
 * TODO: need to test the new brightness functions
 */
//PlanarImage pi = JAI.create("fileload", args[0]);
/* there are going to be 4 quadrants */
/*  (x,y)
 * (0,0)     (.5*w,0)      (w,0)
 * +----------+------------+
 * |                       |
 * |   0             1     |
 * |                       |
*(0,.5h)+----------+------------+(w,.5h)
 * |        (.5h,.5w)      |
 * |                       |
 * |   2              3    |
 * |                       |
 * +----------+------------+ (w,h)
 * (0,h)      (.5w, h)
 */
public class SunlightSensor {
	
	PlanarImage pImage;
	Rectangle [] quadrants;
	public static void main(String [] args) {	
	}
	public SunlightSensor (PlanarImage pi) {
		/* TODO:		 * the user of the class should be able to pass in an image ..*/
		this.pImage = pi;
	
		int m_y = pi.getMinY();
		int m_x = pi.getMinX();
		int M_y = pi.getMaxY();
		int M_x = pi.getMaxX();
		
		// quadrant 0
		this.quadrants[0].add(m_x,      m_y);
		this.quadrants[0].add(.5*M_x,   m_y);
		this.quadrants[0].add(m_x,   .5*M_y);
		this.quadrants[0].add(.5*M_x,.5*M_y);
		
		// quadrant 1
		this.quadrants[1].add(.5*M_x,   m_y);
		this.quadrants[1].add(M_x,      m_y);
		this.quadrants[1].add(.5*M_x,.5*M_y);
		this.quadrants[1].add(M_x,   .5*M_y);
		
		// quadrant 2
		this.quadrants[2].add(m_x,    .5*M_y);
		this.quadrants[2].add(.5*M_x, .5*M_y);
		this.quadrants[2].add(m_x,       M_y);
		this.quadrants[2].add(.5*M_x,    M_y);
		
		// quadrant 3
		this.quadrants[3].add(.5*M_x, .5*M_y);
		this.quadrants[3].add(.5*M_x,   m_y);
		this.quadrants[3].add(M_x,   .5*M_y);
		this.quadrants[3].add(M_x,      M_y);
	}
	
	public double how_bright (int i) throws Exception {
		
		if (i < 0 || i > 3) {
			return -1;
		}
		
		Raster image = this.pImage.getData(quadrants[i]);
		Point3D WHITE = new Point3D(255,255,255);
		
		double brightness = 0;
		int num_pixels = (int) quadrants[i].getMaxX() * (int) quadrants[i].getMaxY();
		
		for (int x = (int) quadrants[i].getMinX(); x <= quadrants[i].getMaxX(); x++) {
			for (int y = (int) quadrants[i].getMinY(); y <= quadrants[i].getMaxY(); y++) {
				int [] dArray = new int[3];
				Point3D color = new Point3D();
				
				image.getPixel(x, y, dArray);
				color.x = dArray[0];
				color.y = dArray[1];
				color.z = dArray[2];
				
				double dist = WHITE.distance(color);
				brightness += dist;
			}
		}
		return brightness / num_pixels;
	}
	
	public int get_brightest() throws Exception {
		int b_i = 0;
		double brightness = how_bright(0);
		
		for (int i = 1; i < quadrants.length; i++) {
			double temp = how_bright(i);
			if (temp > brightness) {
				b_i = i;
				brightness = temp;
			}
		}
		return b_i;
	}
	/* TODO: write function called get brightest quadrant */
}
